
// 懒加载：
//      只加载当前可视区域的图片
// 封装一个方法，用于获取任何一个元素到文档的距离
window.onload = function () {
    var divs = document.querySelectorAll('body > div');
    // 可视区的高度
    var clientH = document.documentElement.clientHeight;
    // 加载的时候执行一次
    auto();
    window.onscroll = auto;
    function auto() {
        // 滚动条的高度
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        for (var i = 0; i < divs.length; i++) {
            var item = divs[i];
            var top = getPos(item).top;
            if(top<=clientH){
                item.style.visibility = "visible";
            }else {
                item.style.visibility = "hidden";
            }
            if (top + item.clientHeight/2 <= clientH + scrollTop && top>=clientH) {
                if(i%2==0){
                    item.style.animation = ' fadeIn .5s linear 1 0s';
                item.style.visibility= 'visible';
                }else {
                    item.style.animation = 'fadeIn .5s linear 1 0s';
                item.style.visibility= 'visible';
                }  
            }else{
                item.style.animation="none";
            }
        }
    }

}
function getPos(ele) {
    var l = 0;
    var t = 0;
    while (ele) {
        l += ele.offsetLeft;
        t += ele.offsetTop;
        ele = ele.offsetParent;
    }
    return {
        left: l,
        top: t
    }
}